<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\LogCollectionResource;
use App\Models\Log;
use Illuminate\Http\Request;

class LogController extends Controller
{
    /**
     * 登录日志
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $params = $request->all();

        $logs = Log::with('user:id,name')
            ->when($params['ip'] ?? '', function ($query, $ip) {
                $query->where('ip', $ip);
            })
            ->when($params['status'] ?? 0, function ($query, $status) {
                $query->where('status', $status);
            })
            ->when($params['name'] ?? '', function ($query, $name) {
                $query->whereHas('user', function ($query) use ($name) {
                    $query->where('name', 'like', '%' . $name . '%');
                });
            })
            ->orderBy('id', 'desc')
            ->paginate($params['size'] ?? 10);

        return $this->success(new LogCollectionResource($logs));
    }
}
